package com.ucenter.server.org.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ucenter.server.org.dao.DepInfoDao;
import com.ucenter.server.org.pojo.DepInfo;
import com.ucenter.server.org.service.DepInfoService;
import com.system.comm.model.Page;
import com.system.comm.utils.FrameNoUtil;
import com.system.comm.utils.FrameStringUtil;
import com.system.handle.model.ResponseFrame;
import com.system.handle.model.ResponseCode;

/**
 * dep_info的Service
 * @author autoCode
 * @date 2018-12-01 09:50:14
 * @version V1.0.0
 */
@Component
public class DepInfoServiceImpl implements DepInfoService {

	@Autowired
	private DepInfoDao depInfoDao;
	
	@Override
	public ResponseFrame saveOrUpdate(DepInfo depInfo) {
		ResponseFrame frame = new ResponseFrame();
		if(FrameStringUtil.isEmpty(depInfo.getDepId())) {
			depInfo.setDepId(FrameNoUtil.uuid());
			depInfoDao.save(depInfo);
		} else {
			depInfoDao.update(depInfo);
		}
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	@Override
	public DepInfo get(String depId) {
		return depInfoDao.get(depId);
	}

	@Override
	public ResponseFrame pageQuery(DepInfo depInfo) {
		depInfo.setDefPageSize();
		ResponseFrame frame = new ResponseFrame();
		int total = depInfoDao.findDepInfoCount(depInfo);
		List<DepInfo> data = null;
		if(total > 0) {
			data = depInfoDao.findDepInfo(depInfo);
		}
		Page<DepInfo> page = new Page<DepInfo>(depInfo.getPage(), depInfo.getSize(), total, data);
		frame.setBody(page);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}
	
	@Override
	public ResponseFrame delete(String depId) {
		ResponseFrame frame = new ResponseFrame();
		depInfoDao.delete(depId);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	@Override
	public List<DepInfo> findByOrgId(String orgId) {
		List<DepInfo> data = new ArrayList<DepInfo>();
		List<DepInfo> list = depInfoDao.findByOrgId(orgId);
		for (DepInfo dep1 : list) {
			if ("0".equals(dep1.getParentDepId())) {
				dep1.setLevel(1);
				data.add(dep1);
				for (DepInfo dep2 : list) {
					if (dep1.getDepId().equals(dep2.getParentDepId())) {
						dep2.setLevel(2);
						data.add(dep2);
						for (DepInfo dep3 : list) {
							if (dep2.getDepId().equals(dep3.getParentDepId())) {
								dep3.setLevel(3);
								data.add(dep3);
								for (DepInfo dep4 : list) {
									if (dep3.getDepId().equals(dep4.getParentDepId())) {
										dep4.setLevel(4);
										data.add(dep4);
									}
								}
							}
						}
					}
				}
			}
		}
		for (DepInfo dep : list) {
			if (dep.getLevel() == 1) {
				dep.setTipName(dep.getName());
			} else if (dep.getLevel() == 2) {
				dep.setTipName("|-- " + dep.getName());
			} else if (dep.getLevel() == 3) {
				dep.setTipName("|---- " + dep.getName());
			} else if (dep.getLevel() == 4) {
				dep.setTipName("|------ " + dep.getName());
			} else {
				dep.setTipName(dep.getName());
			}
		}
		return data;
	}
}